// pages/destination/destination.js
var amapFile = require('../../utils/amap-wx.js');
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commonAddress: [],
    historyAddress: [],
    tips: {},
    showKeyWords: false,
    pageNumber: 1,
    pageSize: 2,
    noCommonAddress: true
  },
  // 获取相关地址
  bindInput: function (e) {
    console.log(e);
    var that = this;
    var keywords = e.detail.value;
    var key = '14c7d5a7fe1f44284eabe3eb47800d59';
    var myAmapFun = new amapFile.AMapWX({ key: key });
    if (keywords == '') {
      this.setData({
        tips: [],
        showKeyWords: false
      })
    } else {
      myAmapFun.getInputtips({
        keywords: keywords,
        location: '',
        success: function (data) {
          console.log(data);
          if (data && data.tips) {
            that.setData({
              tips: data.tips,
              showKeyWords: true
            });
          }
        }
      })
    }
  },
  // 跳转地图
  bindSearch: function (e) {
    var that = this;
    console.log(e);
    var keywords = e.currentTarget.dataset.keywords;
    var location = e.currentTarget.dataset.location;
    var address = e.currentTarget.dataset.address;
    var historyAddress = this.data.historyAddress;
    for(var i = 0; i < historyAddress.length; i ++) {
      if (historyAddress[i].name == keywords) {
        that.data.historyAddress.splice(i, 1)
      }
    }
    var addList = {
      name: keywords,
      address: address,
      location: location
    }
    console.log('--------------------------------')
    console.log(historyAddress);
    historyAddress.push(addList);
    historyAddress = historyAddress.reverse();
    historyAddress = historyAddress.slice(0, 5);
    console.log(historyAddress);
    // 存取搜索历史
    this.setData({
      historyAddress: historyAddress
    })
    wx.setStorageSync('historyAddress', this.data.historyAddress);
    this.setData({
      historyAddress: wx.getStorageSync('historyAddress')
    })
    // 跳转
    var url = '../index/index?keywords=' + keywords + '&location=' + location;
    wx.reLaunch({
      url: url
    })
  },
  // 获取收藏列表
  getCollection() {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    var collection = this.data.collection;
    var data = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'memberId': globalData.memberId
    }
    console.log(data);
    util.req('/collect/myCollect', data, function (res) {
      console.log(res);
      if (res.resultCode == 200) {
        if(res.resultContent.length == 0) {
          that.setData({
            noCommonAddress: true
          })
        } else {
          that.setData({
            commonAddress: res.resultContent,
            noCommonAddress: false
          })
        }
      } else {
        wx.showToast({
          title: res.resultMsg,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('historyAddress')  != '') {
      this.setData({
        historyAddress: wx.getStorageSync('historyAddress')
      })
    }
    this.getCollection();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})